package com.dylhsh.fkglxt.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dylhsh.fkglxt.mapper.LfdjbMapper;
import com.dylhsh.fkglxt.model.Lfdjb;
import com.dylhsh.fkglxt.service.lfdjbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Service
public class lfdjbServiceImpl implements lfdjbService{

    @Autowired
    LfdjbMapper mapper;

    @Autowired
    RedisTemplate<Object,Object> template;
    @Override
    public List<Lfdjb> getLfdjbList() {
        template.setKeySerializer(new StringRedisSerializer());
        List<Lfdjb> list = (List<Lfdjb>) template.opsForValue().get("lfdjbList");
        if (list==null){
            list = (List<Lfdjb>) template.opsForValue().get("lfdjbList");
            if (list==null){
                synchronized (this){
                    list = mapper.getLfdjbList();
                    template.opsForValue().set("lfdjbList",list);
                }
            }
        }
        return list;
    }

    @Override
    public Lfdjb getLfdjbById(int id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int insLfdjb(Lfdjb lfdjb) {
        template.setKeySerializer(new StringRedisSerializer());
        int i = 0;
        i = mapper.insertSelective(lfdjb);
        if (i!=0){
            List<Lfdjb> list = mapper.getLfdjbList();
            template.opsForValue().set("lfdjbList",list);
        }
        return  i ;
    }

    @Override
    public int delLfdjb(int id) {
        template.setKeySerializer(new StringRedisSerializer());
        int i = 0;
        i = mapper.deleteByPrimaryKey(id);
        if (i!=0){
            List<Lfdjb> list = mapper.getLfdjbList();
            template.opsForValue().set("lfdjbList",list);
        }
        return  i ;
    }

    @Override
    public int upLfdjb(Lfdjb lfdjb) {
        template.setKeySerializer(new StringRedisSerializer());
        int i = 0;
        i = mapper.updateByPrimaryKeySelective(lfdjb);
        if (i!=0){
            List<Lfdjb> list = mapper.getLfdjbList();
            template.opsForValue().set("lfdjbList",list);
        }
        return  i ;
    }
}
